<?php
namespace Crm\Domain;

use Crm\Common\MetaData as MetaData;

class Log
{
    protected $session; //当前用信息[上下文]

    /**
     * pdo连接。
     *
     * @var object
     * @access pdo
     */
    public $pdo;

    public $metaData;
    public $baseCode;
    public $city_code;
    public $province_code;
    public $subject_option;
    public $subject_key;
    public $wi_option;

    public function __construct()
    {
        $this->pdo = \PhalApi\DI()->pdo;
        $this->baseCode = (object)\PhalApi\DI()->config->get('basecode');
        $this->province_code = \PhalApi\DI()->config->get('app.provinceCode');//省
        $this->city_code = \PhalApi\DI()->config->get('app.cityCode');//城市

        $this->metaData = new MetaData();

        $this->subject_option = $this->metaData->getPairBaseCodeByType(241,' order by sort desc',false);
        $this->subject_key = $this->metaData->getPairBaseCodeByType(241,' order by sort desc');
        $this->wi_option = $this->metaData->getPairBaseCodeByType(10108,' order by sort desc');
    }

    /* 新增日志 */
    public function addLog($user){
        $this->session = (object)$user;
        $params = \PhalApi\DI()->request->getAll();

        $sys_log_id = isset($params['sys_log_id'])?$params['sys_log_id']:0;//系统日志表id
        $is_reply = isset($params['is_reply'])?$params['is_reply']:0;

        $r = isset($params['r'])?$params['r']:'';//日志关系表
        $r_id = intval(isset($params["id"])?$params["id"]:0);
        unset($params["id"]);

        //----------- 此次需要优化, 房源与客源主题不一样 开始
        switch ($r){
            case 'customer':
                $r_table = 'crm_customer';
                $params['subject'] = $this->subject_key[$params['subject']];
                break;
            case 'esf':
                $r_table = 'fc_esf';
                $params['subject'] = $this->subject_key[$params['subject']];
                break;
            default:
                break;
        }
        //----------- 此次需要优化 结束

        $params['create_date'] = isset($params['create_date'])?strtotime($params['create_date']):time();
        $params['create_uid'] = $this->session->user_id;
        $params['write_uid'] =  $this->session->user_id;

        if($r_id!=0 && $r!='') {
            //验证至少10个汉字
            if (mb_strlen($params['content']) < 9) {
                $result['msg'] = '操作失败,请填至少10个汉字字!';
                $result['status'] = 'no';
                return $result;
            }
            //添加日志
            $log_id = $this->pdo->add($params, 'crm_log');
            //添加关系表
            if ($log_id) {
                $rlt = $this->pdo->add(array($r.'_id' => $r_id, 'log_id' => $log_id), 'crm_r_' . $r . '_log');
                if ($rlt) {
                    //更新主表-跟进时间
                    $this->pdo->update(array('log_date' => time()), $r_table, 'id=' . $r_id);
                    if($is_reply && $sys_log_id!=0) {
                        //修改sys_action_log的status状态为1
                        $this->pdo->update(array('status' => 1), 'sys_action_log', 'id=' . $sys_log_id);
                    }
                    $result['id'] = $log_id;
                    $result['status'] = 'ok';
                } else {
                    $result['msg'] = '操作失败,请填至少10个汉字字!';
                    $result['status'] = 'no';
                }
            }
        }else{
            $result['msg'] = '操作失败,数据非法!';
            $result['status'] = 'no';
        }
        return $result;
    }

    /* 获取日志信息 */
    public function logList($r_id,$r,$limit=0,$size=20){
        $table = "crm_r_".$r."_log";
        $id = $r."_id";
        $sql = "select b.* from ".$table." as a left join crm_log as b on a.log_id=b.id where b.flag=1 and a.".$id."=".$r_id." order by a.id desc";
        $res = $this->pdo->getAll($sql);

        $this->loadModel('sys_user');
        foreach ($res as $key=>$Info){
            //创建者
            $create_user = $this->sys_user->getUserNameById($Info['create_uid']);
            $res[$key]['create_name'] = $create_user['true_name'];
            //跟进时间
            $res[$key]['create_date_val'] = date('Y-m-d H:i:s',$Info['create_date']);
        }
        return $res;
    }

    /* 获取日志信息 */
    public function logList2($data){
        $where = " a.flag=1";
        //创建者
        $create_uid = isset($data['create_uid'])?$data['create_uid']:0;
        if($create_uid != 0){
            $where .= " and a.create_uid = ".intval($create_uid);
        }
        //开始时间
        $start_date = isset($data['start_date'])?$data['start_date']:0;
        if($start_date != 0){
            $where .= " and a.create_date >= ".$start_date;
        }
        //结束时间
        $end_date = isset($data['end_date'])?$data['end_date']:0;
        if($end_date != 0){
            $where .= " and a.create_date <= ".$end_date;
        }
        //跟进类型
        $subject = isset($data['subject'])?$data['subject']:'';
        if($subject != ''){
            $where .= " and a.subject = '".$subject."'";
        }
        //echo date('Y-m-d H:i:s',$start_date).'-'. date('Y-m-d H:i:s',$end_date).'----';
        $sql = "SELECT * FROM crm_log as a  WHERE ".$where;
        //if($create_uid==4)echo $sql;
        $rlt = $this->pdo->getAll($sql);

        //todo 后期优惠后可以不需要下面代码
        foreach ($rlt as $key=>$val){
            $rlt[$key]['create_date_val'] = date('Y-m-d H:i:s', $val['create_date']);
            $sql = "select * from crm_r_esf_log where log_id=".$val['id'];
            $esf = $this->pdo->getRow($sql);
            if(isset($esf['id']) && $esf['id']>0){
                $this->loadModel('fc_esf');
                $Info = $this->fc_esf->getEsfBaseInfo($esf['esf_id']);
                $rlt[$key]['type_val'] = '房源';
                $rlt[$key]['data_val']= $Info['project_name'].$Info['build_no'].'栋'.$Info['unit_no'].'单元'. $Info['house_no'].'号';
                $rlt[$key]['url'] = '/fc_esf/show?id='.$esf['esf_id'].'&house_type='.$Info['house_type'];
            }else{
                $sql = "select * from crm_r_customer_log where log_id=".$val['id'];
                $customer = $this->pdo->getRow($sql);
                if(isset($customer['id']) && $customer['id']>0){
                    $this->loadModel('crm_customer');
                    $Info = $this->crm_customer->getCustomerInfoById($customer['customer_id'],'base');
                    $rlt[$key]['type_val'] = '客源';
                    $rlt[$key]['data_val'] = $Info['name'];
                    $rlt[$key]['url'] = '/crm_customer/show?id='.$customer['customer_id'];
                }
            }
        }
        return $rlt;
    }

}

